package com.kevis.zhunblog.admin.controller.modules.sys;

import com.kevis.webCommon.constant.ControllerConstant;
import com.kevis.zhunblog.admin.controller.base.BaseController;
import com.kevis.zhunblog.admin.security.entity.SecurityUser;
import com.kevis.zhunblog.core.dto.sys.SysMenu;
import com.kevis.zhunblog.service.PowerMenuService;
import com.kevis.zhunblog.service.RolePowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SysMenuController
 *
 * @Author kevis
 * @Date 2023/3/31 10:33
 */
@RestController
@RequestMapping(ControllerConstant.API_SYSTEM_PREFIX + "menu/")
public class SysMenuController extends BaseController {

    private String MODULE_PATH = "/system/menu/";

    @Autowired
    private PowerMenuService powerMenuService;

    @Autowired
    private RolePowerService rolePowerService;


    @PostMapping("data")
    public List<SysMenu> getUserMenu() {
        Object authentication = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //String userName = ((UserDetails) authentication).getUsername();
        int roleId = ((SecurityUser) authentication).getRoleId();
        //List<SysMenu> menuList = powerMenuService.getUserMenu(userName);
        List<SysMenu> menuList = rolePowerService.getUserMenu(roleId);
        List<SysMenu> r = powerMenuService.toTreeUserMenu(menuList, 0);
        return r;
    }
}
